<?php

class FP_Image_TTF extends FP_Image
{
  const LEFT = 'left';
  const CENTER = 'center';
  const RIGHT = 'right';
  const TOP = 'top';
  const BOTTOM = 'bottom';
  const MANUAL = 'manual';

  protected $leading = 0;

  protected $text = null;
  protected $angle = 0;
  protected $font_url = null;
  protected $font_size = 10;

  protected $justify = self::CENTER;
  protected $valign = self::CENTER;

  protected $text_x = 0;
  protected $text_y = 0;

  protected $foreground_color = array(255,255,255);
  protected $background_color = array(0,0,0);

  public function __construct($pText, $pFontUrl, array $pOptions=array(), $pDestination=null){

    $this->text = $pText;
    $this->font_url = $pFontUrl;

    if (array_key_exists('leading', $pOptions))
      $this->leading = $pOptions['leading'];

    if (array_key_exists('width', $pOptions))
      $this->width = $pOptions['width'];

    if (array_key_exists('height', $pOptions))
      $this->height = $pOptions['height'];

    if (array_key_exists('justify', $pOptions))
      $this->justify = $pOptions['justify'];

    if (array_key_exists('valign', $pOptions))
      $this->valign = $pOptions['valign'];

    if (array_key_exists('angle', $pOptions))
      $this->angle = $pOptions['angle'];

    if (array_key_exists('font_size', $pOptions))
      $this->font_size = $pOptions['font_size'];

    if (array_key_exists('x', $pOptions) && $this->justify == self::MANUAL)
      $this->text_x = $pOptions['x'];

    if (array_key_exists('y', $pOptions) && $this->justify == self::MANUAL)
      $this->text_y = $pOptions['y'];

    if (array_key_exists('foreground_color', $pOptions))
      $this->foreground_color = $pOptions['foreground_color'];

    if (array_key_exists('background_color', $pOptions))
      $this->background_color = $pOptions['background_color'];

    $textbox = imagettfbbox($this->font_size, $this->angle, $this->font_url, $this->text);

    $this->width = (!$this->width) ? abs($textbox[2] - $textbox[0]) : $this->width;
    $this->height = (!$this->height) ? abs($textbox[5] - $textbox[3]) + (($this->line_count > 1 ) ? ($this->line_count * $this->leading) : 0) : $this->height;

    $this->image = imagecreate($this->width, $this->height);

    $bg_color = imagecolorallocate($this->image, $this->background_color[0],  $this->background_color[1],  $this->background_color[2]);
    $fg_color = imagecolorallocate($this->image, $this->foreground_color[0],  $this->foreground_color[1],  $this->foreground_color[2]);

    $lines = split("[\n]+", $this->text);
    $this->line_count = count($lines);

    $box = imagettfbbox($this->font_size, 0, $this->font_url, $lines[0]);
    $line_height = $box[3] - $box[5];

    $total_height = 0;
    for ($i=0; $i < $this->line_count; $i++){
      $total_height += $this->leading;
      $box = imagettfbbox($this->font_size, 0, $this->font_url, $lines[$i]);
      $total_height += $box[3] - $box[5];
    }

    switch ($this->valign){
    case self::MANUAL:
      $y = $this->text_y;
      break;
    case self::TOP:
      $y = $this->leading + $line_height;
      break;
    case self::BOTTOM:
      $y = $this->height - ($total_height + $this->leading);
      break;
    case self::CENTER:
      $y = ($this->height / 2) - ($total_height / 2);
    }

    switch ($this->justify){
    case self::LEFT:
      for ($i=0; $i < $this->line_count; $i++){
	imagettftext($this->image, $this->font_size, $this->angle, $this->text_x, $y, $fg_color, $this->font_url, $this->text);
	$y += $height + $this->leading;
      }
      break;
    case self::RIGHT:
      for ($i=0; $i < $this->line_count; $i++){
	$box = imagettfbbox($this->font_size, 0, $this->font_url, $lines[$i]);
	$line_height = $box[3] - $box[5];
	$line_width = $box[2] -$box[0];

	$x = $this->width - $line_width;

	imagettftext($this->image, $this->font_size, $this->angle, $x, $y, $fg_color, $this->font_url, $lines[$i]);
	$y += $line_height + $this->leading;
      }
      break;
    case self::CENTER:
    default:
      for ($i=0; $i < $this->line_count; $i++){
	$box = imagettfbbox($this->font_size, 0, $this->font_url, $lines[$i]);
	$line_height = $box[3] - $box[5];
	$line_width = $box[2] -$box[0];

	$x = ($this->width / 2) - ($line_width / 2);

	imagettftext($this->image, $this->font_size, $this->angle, $x, $y, $fg_color, $this->font_url, $lines[$i]);
	$y += $line_height + $this->leading;
      }

      break;
    }

    if ($pDestination)
      $this->save($pDestination);

  }

}

?>